package com.sxz.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.sxz.mall.product.entity.AttrEntity;
import com.sxz.mall.product.service.AttrAttrgroupRelationService;
import com.sxz.mall.product.service.AttrService;
import com.sxz.mall.product.service.CategoryService;
import com.sxz.mall.product.vo.AttrGroupRelationVO;
import com.sxz.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sxz.mall.product.entity.AttrGroupEntity;
import com.sxz.mall.product.service.AttrGroupService;
import com.sxz.mall.common.utils.PageUtils;
import com.sxz.mall.common.utils.R;


/**
 * 属性分组
 *
 * @author shixinzhi
 * @email 395441300@qq.com
 * @date 2024-08-20 15:10:47
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    // product/attrgroup/225/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrgroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", list);
    }

    // attr/relation
    @PostMapping("/attr/relation")
    public R saveBatch(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }

    // attr/relation/delete
    @PostMapping("attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVO[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    // /6/noattr/relation?t=164&page=1&limit=10&key=
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.getNoAttrRelation(params, attrgroupId);
        return R.ok().put("page", pageUtils);
    }


    // product/attrgroup/5/attr/relation
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrList);
    }


    /**
     * 列表 分页查询，根据catId
     * 前端数据：
     * {
     * page:1
     * limit: 10
     * sidx:"id" // 排序字段
     * order:'asc/desc' // 排序方式
     * key:'小米' // 查询关键字
     * }
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
